package com.example.design.shejimoshi.action.oberver.异步非阻塞;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;

import java.util.List;
import java.util.concurrent.Executors;

public class UserController {
//    private UserService userService;
    private EventBus eventBus;
    private static final int DEFAULT_EVENTBUS_THREAD_POOL_SIZE = 8;

    public UserController() {
        // 同步阻塞的方式
//        eventBus = new EventBus();
        // 异步非阻塞模式
        eventBus = new AsyncEventBus(Executors.newFixedThreadPool(DEFAULT_EVENTBUS_THREAD_POOL_SIZE));
    }

    public void setResObserver(List<Object> observers) {
        for (Object observer : observers) {
            // 注册观察者
            eventBus.register(observer);
        }
    }

    public long register (String telephone, String password) {
//        long userId = userService.register(telephone, password);
        long userId = 0L;

        // 用来给观察者发送事件
        eventBus.post(userId);
        return userId;
    }

}
